{% extends "master.html" %}
{% block subtitle %} - 出入库管理 - 分拆出库单{% endblock %}

{% block extCSS %}
<style type="text/css">
    .template,.atmtemplate{
        display : none;
    }
</style>
{% endblock %}


{% block extJS %}
<script src="{{url_for('static', filename='js/customize/ivtnt_add.js')}}" type="text/javascript"></script>
<script language="JavaScript" type="text/javascript">
//<![CDATA[
    var extPdts = [];
    {% for (d,ivt,ivtip) in data %}
        extPdts.push('{{d.pdt.no}}');
    {% endfor %}    

    function toSubmit(){
        var msg = [];
        clearMsg();
        $(".err").removeClass("err");
        
        var atleastone = false;
        var qtyinput = true;
        var qtyok = true;
        var totalQty = 0;
        $("input[name^='oldqty_']").each(function(){
            var t = $(this);
            var max = parseInt(t.attr('maxqty'));
            
            if(!t.val()){
                qtyinput = false;
                t.addClass('err');
            }else if(!check_number(t.val())){
                msg.push('请正确填写数量,必须为数字！')   
            }else{
                var q = parseInt(t.val());
                if(q > max){
                    qtyok = false;
                    t.addClass('err');
                }else{
                    totalQty += q;
                }
            }
            atleastone = true;          
        })
        
        if(!atleastone){
            msg.push('请填写至少一个商品！')
        }

        if(!qtyinput){
            msg.push('请填写出库数量！');
        }
        
        if(!qtyok){
            msg.push('请正确填写数量，分拆出库数量必须少于原出库数量！');
        }
        
        var checkedItem = $("input[name^='item_']:checked").length;
        
        if(msg.length > 0){
            alertError(msg);
            return false;
        }else{
            if( totalQty != checkedItem){
                if(confirm('选中的个体商品的数量与所填数量不相符，会导致无法确认收货，继续保存吗？')){
                    $("form").submit();
                }
            }else{
                $("form").submit();  
            }            
            
        }
        
    }
    
    function toCancel(){
        redirect_alert("{{MSG_LEAVE_WITHOUT_SAVING}}","{{url_for('bpIvtnt.view')}}")
    }
    
    function selectallitem(obj){
        var t = $(obj);
        var div = $(t.parents(".row")[0]);
        if(t.attr('checked')){
            $("input[type='checkbox']",div).attr('checked','checked');
        }else{
            $("input[type='checkbox']",div).removeAttr('checked');
        }
    }

//]]>
</script>
{% endblock %}


{% block main %}
<div class="container">
    <div class="row">
        <ol class="breadcrumb">
          <li><a href="{{url_for('bpRoot.view')}}">主面板</a></li>
          <li><a href="{{url_for('bpIvtnt.view')}}">出入库管理</a></li>
          <li class="active">分拆出库单</li>
        </ol>
    </div>
    <div class="row">
        <div class="btn-toolbar pull-right">
            <div class="btn-group"><button class="btn btn-primary" onclick="toSubmit()">{{BTN_SUBMIT}}</button></div>
            <div class="btn-group"><button class="btn btn-inverse" onclick="toCancel()">{{BTN_CANCEL}}</button></div>
        </div>
    </div>
    <div class="row">    
        <form class="" method='POST' action="{{url_for('.view',action='split')}}" enctype="multipart/form-data">
        <input type="hidden" name="id" value="{{obj.id}}"/>
        <input type="hidden" name="direction" id="direction" value="{{IVTNT_OUT}}"/>
        <fieldset>
            <legend>出库单详情</legend>
            <div class="col-md-4">
                <div class="control-group controls-row">
                    <label for="remark" class="control-label"><label for="refer">出库到&nbsp;[{{obj.ivt.name|ss(c='IVTNAME')}}]</label></label>
                    <div class="controls">
                        &nbsp;                       
                    </div>
                </div>
                <div class="control-group controls-row">
                    <label for="remark" class="control-label"><label for="refer">涉及</label></label>
                    <div class="controls">
                        {{ mymacros.render_goto_popup(obj.refer) }}
                    </div>
                </div>
                <div class="control-group controls-row">
                    <label for="remark" class="control-label"><label for="remark">备注</label></label>
                    <div class="controls"><textarea name="remark" id="remark">{{obj.remark|f}}</textarea></div>
                </div>
            </div>   
            <div class="col-md-4">
                <div class="control-group controls-row">
                    <label for="remark" class="control-label"><label for="createTime">开单时间</label></label>
                    <div class="controls"><input type="text" name="createTime" id="createTime" class="datepicker" value="{{now()|fd}}"/></div>
                </div>
                <div class="control-group controls-row">
                    <label for="remark" class="control-label"><label for="createTime">经办人</label></label>
                    <div class="controls"><input type="text" value="{{session['user_profile']['name']}}" readonly="true"/></div>
                </div>
            </div>
            <div class="col-md-4">
                <div class="control-group controls-row">
                  <label class="control-label" for="attachment">附件</label>
                  <div class="controls">
                      <ul id="atmul" class="list-unstyled">
                          <li><input type="file" name="atm_10"/><a href="#" onclick="addAtm('atmtemplate','atmmk','#atmul')"><i class="glyphicon glyphicon-plus"></i></a></li>
                          <li class="atmtemplate"><input type="file" name="atm_x"/><a href="#" onclick="delAtm(this,'atmmk')"><i class="glyphicon glyphicon-minus"></i></a></li>
                      </ul>
                  </div>
                </div>
            </div>
            <div class="col-md-12">
                <table class="table table-bordered">
                    <thead>
                        <tr>
                            <td class="col-md-1">&nbsp;</td>
                            <td class="col-md-3">商品编号</td>
                            <td class="col-md-2">商品名称</td>
                            <td>所在仓位</td>
                            <td class="col-md-1">仓存数量</td>
                            <td class="col-md-1">出库数量</td>
                            <td class="col-md-2">分拆数量</td>
                            <td class="col-md-2">备注</td>
                        </tr>
                    </thead>
                    <tbody id="pdtlist">
                        {% for (d,ivt,ivtip) in data %}
                            <tr>
                                <td><a href="#" onclick="rmProduct(this)"><i class="glyphicon glyphicon-remove"></i></a></td>          
                                <td><a href="{{url_for('bpPdt.view',action='view',id=d.pdt.id)}}" target="_blank">{{d.pdt.no}}</a></td>
                                <td>{{d.pdt.name}}</td>
                                <td>{{ivt.fullPath}}</td>
                                <td>{{ivtip.qty}}</td>
                                <td>{{d.qty}}</td>
                                <td><input type="text" name="oldqty_{{d.id}}" value="{{d.qty}}" class="num col-md-1" maxqty="{{d.qty}}"/></td>
                                <td><textarea name="oldremark_{{d.id}}" class="col-md-2">{{d.remark|f}}</textarea></td>
                            </tr>
                        {% endfor %}
                    </tbody>                
                </table>
            </div>
        </fieldset>
        <fieldset>
            <legend>涉及的商品个体明细</legend>
            {% for (d,ivt,ivtip) in data %}
            {% set key = [d.pdtID,ivt.id]|join('_') %}
            {% set tmp = items[key] %}
            <div class="row">
               <div class="col-md-12">在仓位[<strong>{{ivt.fullPath}}</strong>]中与商品[{{mymacros.render_thumb(d.pdt)}}<strong>{{d.pdtno}}</strong>]的相关物品 : </div>
               <div class="col-md-3"><label class="checkbox"><input type="checkbox" onclick="selectallitem(this)">全选</label></div>
               {% for item in tmp %}
                    {% set item_name = ['item',d.pdtID,d.sIvtLtnID]|join('_') %}
                    <div class="col-md-3">
                        <label class="checkbox">
                              <input type="checkbox" value="{{item.no}}" name="{{item_name}}">
                          {{item.no}}
                        </label>
                    </div>
                {% endfor %}
            </div>
            {% endfor %}
        </fieldset>
        </form>
    </div>
</div>
      
{% endblock %}